/**
 * 
 */
package com.zufangbao.earth.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.zufangbao.earth.entity.security.Principal;
import com.zufangbao.earth.service.PrincipalService;
import com.zufangbao.gluon.opensdk.Md5Util;
import com.zufangbao.sun.entity.company.CompanyApp;
import com.zufangbao.sun.entity.company.corp.App;
import com.zufangbao.sun.service.AppService;
import com.zufangbao.sun.service.CompanyAppService;

/**
 * 
 * @author Downpour
 */
@Service("principalService")
public class PrincipalServiceImpl extends GenericServiceImpl<Principal> implements PrincipalService {
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;
	@Autowired
	private AppService appService;
	@Autowired
	private CompanyAppService companyAppService;
	
	/* (non-Javadoc)
	 * @see com.demo2do.alaska.service.UserService#getPrincipal(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public Principal getPrincipal(String name) {
		List<Principal> principals = genericDaoSupport.searchForList("FROM Principal principal WHERE principal.name = :name ", "name", name);
		return CollectionUtils.isNotEmpty(principals) ? principals.get(0) : null; 
	}
	
	@Override
	public String updatePassword(Principal principal, String oldPassword,
			String newPassword) {
		if(!Md5Util.encode(oldPassword).equals(principal.getPassword())){
			return "原密码输入有误";
		}
		principal.setPassword(Md5Util.encode(newPassword));
		try {
			this.genericDaoSupport.update(principal);
			return "修改成功";
		} catch (Exception e) {
			return "修改失败请重试";
		}
	}

	@Override
	public List<Principal> getRoleListByApp(App app) {
		if(app == null){
			return new ArrayList<Principal>();
		}
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("keyId", app.getId());
		List<Principal> appPrincipals = this.genericDaoSupport.searchForList("from Principal where keyId=:keyId",params);
		return appPrincipals;
	}

	@Override
	public void save(Principal newPrincipal) {
		this.genericDaoSupport.save(newPrincipal);
	}

	@Override
	public List<App> get_can_access_app_list(Principal principal) {
		if (principal.isSuperRole()){
			return getSuperRoleAppList();
		}
		if (principal.isFinanceRole()) {
			return getFianceRoleAppList(principal);
		}
		if (principal.is_app_role()) {
			return getAppRoleAppList(principal);
		}
		
		return Collections.EMPTY_LIST;

	}

	private List<App> getSuperRoleAppList() {
		return appService.loadAll(App.class);
	}

	private List<App> getAppRoleAppList(Principal principal) {
		List<App> appList = new ArrayList<App>();
		App app = this.appService.load(App.class, principal.getKeyId());
		appList.add(app);
		return appList;
	}

	private List<App> getFianceRoleAppList(Principal principal) {
		List<App> appList = new ArrayList<App>();
		List<CompanyApp> companyAppList = getCompanyAppList(principal.getKeyId());
		for (CompanyApp companyApp : companyAppList) {
			App app = this.appService.load(App.class, companyApp.getAppId());
			appList.add(app);
		}
		return appList;
	}
	private List<CompanyApp> getCompanyAppList(Long companyId) {
		return this.companyAppService.getCompanyAppListByCompanyId(companyId);
	}
}
